//
//  PullLayout.m
//  BeautyClother
//
//  Created by dllo on 15/8/21.
//  Copyright (c) 2015年 宋爽. All rights reserved.
//

#import "PullLayout.h"


@interface PullLayout ()
@property (nonatomic, assign) CGFloat offLeft;
@property (nonatomic, assign) CGFloat offRight;

@property (nonatomic, assign) CGFloat offWidth;
@property (nonatomic, assign) CGFloat offHeight;

@property (nonatomic, assign) CGFloat RealWidth;
@end

@implementation PullLayout

- (void)dealloc
{
//    [_array release];
//    [_layoutArr release];
    [super dealloc];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.layoutArr = [NSMutableArray array];
        self.array = [NSMutableArray array];
        
        self.offWidth = [[UIScreen mainScreen]bounds].size.width / 375;
        self.offHeight = [[UIScreen mainScreen]bounds].size.height / 667;
        self.RealWidth = 180 * _offWidth;
    }
    return self;
}

- (void)prepareLayout
{
    [super prepareLayout];
    [self caculatorHeight];
}

- (void)caculatorHeight
{
    self.offLeft = 50 * _offHeight;
    self.offRight = 0;
    self.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    NSInteger num = [self.collectionView numberOfItemsInSection:0];
    for (int i = 0; i < num; i++) {
        NSIndexPath *iiii = [NSIndexPath indexPathForItem:i inSection:0];
        UICollectionViewLayoutAttributes *att = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:iiii];
        NSDictionary *dic = [NSDictionary dictionary];
        dic = [self.layoutArr objectAtIndex:i];
        if ([[[dic objectForKey:@"component"] objectForKey:@"componentType"] isEqualToString:@"calendar"]) {
            

        }else
        {
            CGFloat he = [[dic objectForKey:@"height"] floatValue];
            
        CGFloat height = he * _RealWidth / [[dic objectForKey:@"width"]floatValue] + 40;
        if (i % 2 == 0) {
            att.frame = CGRectMake(5, 5 + _offLeft, _RealWidth, height);
            _offLeft += height;
        }else
        {
            att.frame = CGRectMake(190 * _offWidth, 10 + _offRight, _RealWidth, height);
            _offRight += height;
        }
        }
        [self.array addObject:att];
    }
        
}

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
//    NSArray *arr = [super layoutAttributesForElementsInRect:rect];
    NSMutableArray *arr = [NSMutableArray array];
    for (UICollectionViewLayoutAttributes *att in self.array) {
        if (CGRectContainsRect(att.frame, rect) || CGRectIntersectsRect(att.frame, rect)) {
            [arr addObject:att];
        }
    }
    return arr;
}

- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return [self.array objectAtIndex:indexPath.item];
}

- (CGSize)collectionViewContentSize
{
    if (_offRight > _offLeft) {
        return CGSizeMake(350 * _offWidth, _offRight);
    }
    else
    {
        return CGSizeMake(350 * _offWidth, _offLeft);
    }
}

@end
